<template>
	<!-- 靠近开锁 -->
	<view>
		<view class="u-flex u-row-around p-30">
			<view class="small-btn" @click="onModeChange">{{isHighOption?'普通选项':'高级选项'}}</view>
			<view class="small-btn" @click="setDefaultInfo">恢复默认</view>
			<view class="small-btn">参数说明</view>
		</view>
		<view class="item-box">
			<view class="">靠近开锁功能</view>
			<u-switch v-model="form.unlockFlag"></u-switch>
		</view>
		<view class="item-box">
			<view class="item-title">蓝牙配对密码</view>
			<input class="u-text-right" type="text" v-model="form.matePassword" placeholder="请输入密码" />
		</view>
		<view class="item-box">
			<view class="item-title">开锁动作模式</view>
			<view class="">
				<u-radio-group v-model="form.unlockMode" placement="row">
					<u-radio name="1" label="点动"></u-radio>
					<view class="m-l-30">
						<u-radio name="0" label="常通"></u-radio>
					</view>
				</u-radio-group>
			</view>
		</view>
		<view class="item-box">
			<view class="item-title">开锁点动时间(ms)</view>
			<input class="u-text-right" type="number" v-model="form.unlockTime" placeholder="请输入" />
		</view>
		<view class="item-box" v-if="isHighOption">
			<view class="item-title">开锁重复点动间隔时间(ms)</view>
			<input class="u-text-right" type="number" v-model="form.unlockGapTime" placeholder="请输入" />
		</view>
		<view class="item-box" v-if="isHighOption">
			<view class="item-title">开锁重复点动次数</view>
			<input class="u-text-right" type="number" v-model="form.unlockCount" placeholder="请输入" />
		</view>
		<view class="item-box">
			<view class="item-title">关锁动作模式</view>
			<view class="">
				<u-radio-group v-model="form.lockMode" placement="row">
					<u-radio name="1" label="点动"></u-radio>
					<view class="m-l-30">
						<u-radio name="0" label="常通"></u-radio>
					</view>
				</u-radio-group>
			</view>
		</view>
		<view class="item-box">
			<view class="item-title">关锁点动时间(ms)</view>
			<input class="u-text-right" type="number" v-model="form.lockTime" placeholder="请输入" />
		</view>
		<view class="item-box" v-if="isHighOption">
			<view class="item-title">关锁重复点动间隔时间(ms)</view>
			<input class="u-text-right" type="number" v-model="form.lockGapTime" placeholder="请输入" />
		</view>
		<view class="item-box" v-if="isHighOption">
			<view class="item-title">关锁重复点动次数</view>
			<input class="u-text-right" type="number" v-model="form.lockCount" placeholder="请输入" />
		</view>
		<!-- <view class="item-box">
			<view class="item-title">关锁动作延时执行时间(ms)</view>
			<input class="u-text-right" type="number" v-model="form.lockDelayTime" placeholder="请输入" />
		</view>
		<view class="item-box">
			<view class="item-title">蓝牙连接距离(推荐设置:6)</view>
			<input class="u-text-right" type="number" v-model="form.connectDistance" placeholder="请输入" />
		</view> -->
		<view class="item-box">
			<view class="item-title">开锁距离(推荐设置:80)</view>
			<input class="u-text-right" type="number" v-model="form.unlockDistance" placeholder="请输入" />
		</view>
		<view class="item-box">
			<view class="item-title">关锁距离(推荐设置:90)</view>
			<input class="u-text-right" type="number" v-model="form.lockDistance" placeholder="请输入" />
		</view>

		<view class="p-30 u-flex u-row-around">
			<view class="btn" @click="handleSubmit">一键写入</view>
			<view class="btn" @click="handleMatch">开始与设备配对</view>
		</view>
	</view>
</template>

<script>
	import {
		getUuid,
		writeBLECharacteristicValue
	} from "../../utils/common";
	import {getNearInfo,setNearInfo} from "../../utils/storage.js"
	const app = getApp();
	export default {
		data() {
			return {
				isHighOption: false,
				form:{
					matePassword: "123456",
					unlockFlag: false,
					unlockMode: '1',
					lockMode: "1",
					unlockTime: "",
					lockTime: "",
					// lockDelayTime: "",
					// connectDistance: "",
					unlockDistance: "20",
					lockDistance: "20",
					unlockCount: "",
					unlockGapTime: "",
					lockCount: "",
					lockGapTime: ""
				}
			};
		},
		onLoad() {
			this.init();
		},
		methods: {
			init() {
				isHighOption: false;
				const nearInfoList = getNearInfo();
				console.log('nearInfoList: ',nearInfoList);
				let isNoSetted = true;
				if(nearInfoList && nearInfoList.length > 0){
					for (var i = 0; i < nearInfoList.length; i++) {
						if(app.globalData.deviceId == nearInfoList[i].deviceId){
							isNoSetted = false;
							this.form = nearInfoList[i];
						}
					}
				}
				if(isNoSetted){
					this.setDefaultInfo();
				}
			},
			setDefaultInfo(){
				uni.showModal({
					title: '提示',
					content: '确定回复默认设置吗?',
					success: (res) =>{
						if (res.confirm) {
							this.form.founlockFlag = false;
							this.form.matePassword = "123456";
							this.form.unlockMode = "1";
							this.form.unlockTime = "";
							this.form.unlockGapTime = "";
							this.form.unlockCount = "";
							this.form.lockMode = "1";
							this.form.lockTime = "";
							this.form.lockGapTime = "";
							this.form.lockCount = "";
							// this.form.lockDelayTime = "";
							// this.form.connectDistance = 0;
							this.form.unlockDistance = 20;
							this.form.lockDistance = 20;
							this.handleSubmit();
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			onModeChange() {
				this.isHighOption = !this.isHighOption;
				this.unlockGapTime = "";
				this.unlockCount = "";
				this.lockGapTime = "";
				this.lockGapTime = "";
			},
			handleSubmit() {
				const serviceId = getUuid("5508");
				writeBLECharacteristicValue(serviceId, getUuid("0020"), this.form.unlockFlag ? '开启' : '关闭');
				writeBLECharacteristicValue(serviceId, getUuid("0021"), this.form.matePassword);
				writeBLECharacteristicValue(serviceId, getUuid("0022"), this.form.unlockMode == 1 ? '点动' : '常通');
				writeBLECharacteristicValue(serviceId, getUuid("0023"), parseInt(this.form.unlockTime));
				writeBLECharacteristicValue(serviceId, getUuid("002B"), parseInt(this.form.unlockGapTime));
				writeBLECharacteristicValue(serviceId, getUuid("002A"), parseInt(this.form.unlockCount));
				writeBLECharacteristicValue(serviceId, getUuid("0024"), this.form.lockMode == 1 ? '点动' : '常通');
				writeBLECharacteristicValue(serviceId, getUuid("0025"), parseInt(this.form.lockTime));
				writeBLECharacteristicValue(serviceId, getUuid("002D"), parseInt(this.form.lockGapTime));
				writeBLECharacteristicValue(serviceId, getUuid("002C"), parseInt(this.form.lockCount));
				// writeBLECharacteristicValue(serviceId, getUuid("0026"), this.form.lockDelayTime);
				// writeBLECharacteristicValue(serviceId, getUuid("0025"), this.form.connectDistance);
				writeBLECharacteristicValue(serviceId, getUuid("0025"), parseInt(this.form.unlockDistance));
				writeBLECharacteristicValue(serviceId, getUuid("0025"), parseInt(this.form.lockDistance));
				writeBLECharacteristicValue(serviceId, getUuid("FF20"), "", "开锁");
				
				let nearInfoList = getNearInfo();
				if(nearInfoList){
					for (var i = 0; i < nearInfoList.length; i++) {
						if(app.globalData.deviceId == nearInfoList[i].deviceId){
							nearInfoList.splice(i,1);
						}
					}
				}else{
					nearInfoList = [];
				}
				nearInfoList.push({...this.form,deviceId:app.globalData.deviceId});
				setNearInfo(nearInfoList);
			},
			handleMatch() {
				const serviceId = getUuid("5508");
				writeBLECharacteristicValue(serviceId, getUuid("FF21"), "", "配对");
			},
		}
	}
</script>

<style lang="scss" scoped>
	.small-btn {
		width: 180rpx;
		border-radius: 40rpx;
		color: $u-primary;
		border: 1px solid $u-primary;
		padding: 8rpx 0;
		text-align: center;
		font-size: 14px;
	}

	.item-box {
		background-color: white;
		display: flex;
		justify-content: space-between;
		border-bottom: 1px solid #f8f8f8;
		padding: 30rpx;
		align-items: center;
	}

	.item-title {
		// width: 340rpx;
	}

	.btn {
		background-color: $u-primary;
		color: white;
		text-align: center;
		padding: 15rpx;
		border-radius: 15rpx;
		width: 280rpx;

	}
</style>